################################################################################ 
#
# The distribution of hate speech and its implications for content moderation
# PSRM - Replication package
# Table E9
#
################################################################################ 


################################################################################ 
#  LIBRARIES
################################################################################ 

library(readxl)
library(xtable)
library(estimatr)
library(broom)

rm(list = setdiff(ls(), ls(pattern = "^wd|^setsave$")))

################################################################################ 
#   FOLDERS
################################################################################ 

data = read.csv(paste0(wd_data_processed, '/for_analysis/clean_for_analysis.csv'))

data$attrited_tot = ifelse(
  data$attrition==0 & data$not_auth_sofar==0 & data$suspended_sofar==0 & data$protected_sofar==0,
  0, 1)

table(data$attrited_tot)

################################################################################ 
#   Effect of treatment on attrition
################################################################################ 

data$treat_aggregate = factor(data$treat_aggregate, 
                               levels = c("control", "alert", "consequences", "empathy"))


model = lm_robust(attrited_tot ~ as.factor(treat_aggregate), data)
outcome = tidy(model)[c('term', 'estimate', 'std.error', 'p.value', 'outcome')]
outcome$nobs = glance(model)$nobs 
mean_depvar = mean(data$attrited_tot[!is.na(data$treat_aggregate)], na.rm = TRUE)
outcome[c('estimate', 'std.error', 'p.value')] = round(outcome[c('estimate', 'std.error', 'p.value')], 3)
outcome
print(round(mean_depvar, 3))
print(round(model$r.squared, 3))

if (setsave==T){write.csv(outcome, paste0(wd_res, '/tables/tabE9_aggregate.csv'))}

################################################################################ 
#   Effect of treatment on attrition
################################################################################ 

model = lm_robust(suspended_sofar ~ as.factor(treat_aggregate), data)
outcome = tidy(model)[c('term', 'estimate', 'std.error', 'p.value', 'outcome')]
outcome$nobs = glance(model)$nobs
outcome[c('estimate', 'std.error', 'p.value')] = round(outcome[c('estimate', 'std.error', 'p.value')], 3)
outcome
mean_depvar = mean(data$suspended_sofar[!is.na(data$treat_aggregate)], na.rm = TRUE)
print(round(mean_depvar, 3))
print(round(model$r.squared, 3))

if (setsave==T){write.csv(outcome, paste0(wd_res, '/tables/tabE9_suspended.csv'))}


model = lm_robust(protected_sofar ~ as.factor(treat_aggregate), data)
outcome = tidy(model)[c('term', 'estimate', 'std.error', 'p.value', 'outcome')]
outcome$nobs = glance(model)$nobs
outcome[c('estimate', 'std.error', 'p.value')] = round(outcome[c('estimate', 'std.error', 'p.value')], 3)
outcome
mean_depvar = mean(data$protected_sofar[!is.na(data$treat_aggregate)], na.rm = TRUE)
print(round(mean_depvar, 3))
print(round(model$r.squared, 3))

if (setsave==T){write.csv(outcome, paste0(wd_res, '/tables/tabE9_protected.csv'))}


cat("\n====================\n")
cat("Saved Table E9")
cat("\n====================\n")
